//
//  ZDQMoreAddressViewController.m
//  CoreDataAddressBook
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZDQMoreAddressViewController.h"
#import "ZDQCoreDataManager.h"
#import "AddressBook.h"

@interface ZDQMoreAddressViewController ()

@property(nonatomic, retain) UIScrollView *scrollView;
@property(nonatomic, retain) UILabel *nameLabel;
@property(nonatomic, retain) UITextField *nameTextField;
@property(nonatomic, retain) UILabel *sexLabel;
@property(nonatomic, retain) UITextField *sexTextField;
@property(nonatomic, retain) UILabel *numberLabel;
@property(nonatomic, retain) UITextField *numberTextField;
@property(nonatomic, retain) UILabel *QQLabel;
@property(nonatomic, retain) UITextField *QQTextField;
@property(nonatomic, retain) UILabel *phoneNumberLabel;
@property(nonatomic, retain) UITextField *phoneNumberTextField;
@property(nonatomic, retain) UILabel *nativeLabel;
@property(nonatomic, retain) UITextField *nativeTextField;
@property(nonatomic, retain) ZDQCoreDataManager *manager;

@end

@implementation ZDQMoreAddressViewController

- (void)deallo {
    
    [_scrollView release];
    [_nameLabel release];
    [_nameTextField release];
    [_sexLabel release];
    [_sexTextField release];
    [_numberLabel release];
    [_numberTextField release];
    [_QQLabel release];
    [_QQTextField release];
    [_phoneNumberLabel release];
    [_phoneNumberTextField release];
    [_nativeLabel release];
    [_nativeTextField release];
    [_manager release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 40)];
    [self.scrollView addSubview:self.nameLabel];
//    self.nameLabel.layer.borderWidth = 1;
    self.nameLabel.text = @"姓名:";
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    [_nameLabel release];
    
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 25, 200, 30)];
    [self.scrollView addSubview:self.nameTextField];
    self.nameTextField.layer.borderWidth = 1;
    self.nameTextField.layer.cornerRadius = 15;
    self.nameTextField.placeholder = @"请输入姓名";
    self.nameTextField.textAlignment = NSTextAlignmentCenter;
    self.nameTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_nameTextField release];
    self.nameTextField.text = self.Add.name;
    
    self.sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 100, 40)];
    [self.scrollView addSubview:self.sexLabel];
//    self.sexLabel.layer.borderWidth = 1;
    self.sexLabel.text = @"性别:";
    self.sexLabel.textAlignment = NSTextAlignmentCenter;
    [_sexLabel release];
    
    self.sexTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 75, 200, 30)];
    [self.scrollView addSubview:self.sexTextField];
    self.sexTextField.layer.borderWidth = 1;
    self.sexTextField.layer.cornerRadius = 15;
    self.sexTextField.placeholder = @"请输入性别";
    self.sexTextField.textAlignment = NSTextAlignmentCenter;
    self.sexTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_sexTextField release];
    self.sexTextField.text = self.Add.sex;
    
    self.numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 120, 100, 40)];
    [self.scrollView addSubview:self.numberLabel];
//    self.numberLabel.layer.borderWidth = 1;
    self.numberLabel.text = @"学号:";
    self.numberLabel.textAlignment = NSTextAlignmentCenter;
    [_numberLabel release];
    
    self.numberTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 125, 200, 30)];
    [self.scrollView addSubview:self.numberTextField];
    self.numberTextField.layer.borderWidth = 1;
    self.numberTextField.layer.cornerRadius = 15;
    self.numberTextField.placeholder = @"请输入学号";
    self.numberTextField.textAlignment = NSTextAlignmentCenter;
    self.numberTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_numberTextField release];
    self.numberTextField.text = self.Add.number;
    
    
    self.phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 170, 100, 40)];
    [self.scrollView addSubview:self.phoneNumberLabel];
//    self.phoneNumberLabel.layer.borderWidth = 1;
    self.phoneNumberLabel.text = @"电话号码:";
    self.phoneNumberLabel.textAlignment = NSTextAlignmentCenter;
    [_phoneNumberLabel release];
    
    self.phoneNumberTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 175, 200, 30)];
    [self.scrollView addSubview:self.phoneNumberTextField];
    self.phoneNumberTextField.layer.borderWidth = 1;
    self.phoneNumberTextField.layer.cornerRadius = 15;
    self.phoneNumberTextField.placeholder = @"请输入电话号码";
    self.phoneNumberTextField.textAlignment = NSTextAlignmentCenter;
    self.phoneNumberTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_phoneNumberTextField release];
    self.phoneNumberTextField.text = self.Add.phonenumber;

    
    self.QQLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 220, 100, 40)];
    [self.scrollView addSubview:self.QQLabel];
//    self.QQLabel.layer.borderWidth = 1;
    self.QQLabel.text = @"QQ:";
    self.QQLabel.textAlignment = NSTextAlignmentCenter;
    [_QQLabel release];
    
    self.QQTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 225, 200, 30)];
    [self.scrollView addSubview:self.QQTextField];
    self.QQTextField.layer.borderWidth = 1;
    self.QQTextField.layer.cornerRadius = 15;
    self.QQTextField.placeholder = @"请输入QQ";
    self.QQTextField.textAlignment = NSTextAlignmentCenter;
    self.QQTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_QQTextField release];
    self.QQTextField.text = self.Add.qq;
    
    self.nativeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 270, 100, 40)];
    [self.scrollView addSubview:self.nativeLabel];
//    self.nativeLabel.layer.borderWidth = 1;
    self.nativeLabel.text = @"籍贯:";
    self.nativeLabel.textAlignment = NSTextAlignmentCenter;
    [_nativeLabel release];
    
    self.nativeTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 275, 200, 30)];
    [self.scrollView addSubview:self.nativeTextField];
    self.nativeTextField.layer.borderWidth = 1;
    self.nativeTextField.layer.cornerRadius = 15;
    self.nativeTextField.placeholder = @"请输入籍贯";
    self.nativeTextField.textAlignment = NSTextAlignmentCenter;
    self.nativeTextField.clearButtonMode = UITextFieldViewModeAlways;
    [_nativeTextField release];
    self.nativeTextField.text = self.Add.native;
    
    
    UIButton *updateButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.scrollView addSubview:updateButton];
    updateButton.backgroundColor = [UIColor lightGrayColor];
    [updateButton setTitle:@"修改" forState:UIControlStateNormal];
    updateButton.frame = CGRectMake(80, 330, 100, 40);
    [updateButton addTarget:self action:@selector(updateButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.scrollView addSubview:cancelButton];
    cancelButton.frame = CGRectMake(210, 330, 100, 40);
    [cancelButton setTitle:@"删除" forState:UIControlStateNormal];
    cancelButton.backgroundColor = [UIColor lightGrayColor];
    [cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.manager = [ZDQCoreDataManager defaults];
    NSLog(@"url =========== %@", [self.manager applicationDocumentsDirectory]);
    
    
    
}


#pragma mark Button的点击方法
- (void)updateButtonAction:(UIButton *)button {
    
    //获取查询数据的请求,相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"AddressBook"];
    
    //添加一个约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], self.nameTextField.text];
    
    //给请求一个约束
    request.predicate = predicate;
    //通过某个key给数组排序
    //通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    //
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    //先进行查找在修改
    for (AddressBook *add in array) {
        add.sex = self.sexTextField.text;
        add.number = self.numberTextField.text;
        add.phonenumber = self.phoneNumberTextField.text;
        add.qq = self.QQTextField.text;
        add.native = self.nativeTextField.text;
        
    }
    
    [self.manager saveContext];
    
    NSLog(@"%@", array);
  
}





- (void)cancelButtonAction:(UIButton *)button {
    
    //获取查询数据的请求,相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"AddressBook"];
    
    //添加一个约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], self.nameTextField.text];
    
    //给请求一个约束
    request.predicate = predicate;
    //通过某个key给数组排序
    //通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    //
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    for (AddressBook *Add in array) {
        
        [self.manager.managedObjectContext deleteObject:Add];
        
    }
    
    [self.manager saveContext];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
